//
//  UFOPlayTableView.m
//  unrivalledForUniverse
//
//  Created by dllo on 16/1/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UFOPlayTableView.h"
#import "UFOYZXheroCollectionView.h"
#import "UFOheroModel.h"
@interface UFOPlayTableView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, retain) UICollectionView *collectionV;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowL;
@end
@implementation UFOPlayTableView
- (void)dealloc
{
    self.collectionV.delegate = nil;
    self.collectionV.dataSource = nil;
    [_collectionV release];
    [_flowL release];
    [super dealloc];
}
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    
    //  创建collectionView
    self.flowL  = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    _flowL.minimumLineSpacing = 11 * SCREENWIDTH / WIDTHSIXP;
    _flowL.headerReferenceSize = CGSizeMake(3 * SCREENWIDTH / WIDTHSIXP, 10 * SCREENHEIGHT / HEIGHTSIXP);
    _flowL.sectionInset = UIEdgeInsetsMake(0 , 10 * SCREENWIDTH / WIDTHSIXP, 0, 10 * SCREENWIDTH / WIDTHSIXP);
    _flowL.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowL];
    self.collectionV.backgroundColor = [UIColor whiteColor];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.contentView addSubview:self.collectionV];
    [self.collectionV release];
    [self.collectionV registerClass:[UFOYZXheroCollectionView class] forCellWithReuseIdentifier:@"cell"];
}

#pragma 创建TableView坐标
- (void)layoutSubviews
{
    [super layoutSubviews];
    _flowL.itemSize = CGSizeMake(80 * SCREENWIDTH / WIDTHSIXP, 280 * SCREENHEIGHT / HEIGHTSIXP);
    
    self.collectionV.frame = CGRectMake(0, 0,  SCREENWIDTH , 180 * SCREENHEIGHT / HEIGHTSIXP);
    
}

#pragma 接收解析数据的数组
- (void)setContactArr:(NSMutableArray *)contactArr
{
    
    if (_contactArr != contactArr) {
        [_contactArr release];
        _contactArr = [contactArr retain];
    }
    [self.collectionV reloadData];
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UFOheroModel *model = [self.contactArr objectAtIndex:indexPath.row];
    [self.delegate clickHeroImageV:model.mId];
}

#pragma 显示collectionView的数组
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.contactArr.count;
}

#pragma collectionView的重用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UFOYZXheroCollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    UFOheroModel *model = self.contactArr[indexPath.row];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.Pic] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    cell.nameL.text = model.NickName;
    cell.nicknameL.text = model.CName;
    return cell;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
@end
